package com.luli.service.impl;

import com.luli.dao.SortDao;
import com.luli.entity.sort.AdjustOrderReq;
import com.luli.entity.sort.NoticeType;
import com.luli.service.NoticeTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 陆离
 * 2022/7/5 19:57:39
 */
@Service
@Slf4j
public class NoticeTypeServiceImpl implements NoticeTypeService {


    @Autowired
    private SortDao noticeTypeMapper;
    @Override
    public int adjustOrder(AdjustOrderReq orderReq) {
        // 1、查询是否存在分类
        NoticeType noticeType = noticeTypeMapper.selectByPrimaryKey(orderReq.getId());
        log.info("数据--------" + noticeType);
        if(noticeType == null){
            log.info("NoticeServiceImpl.adjustOrder未查询到公告分类！");
            return -2;
        }
        // 2、上移
        if(orderReq.getOperaType().equals(1)){
            //查询下一条记录Id和sort
            Integer nextId = noticeTypeMapper.selectNextId(noticeType);
            NoticeType nextNotice =  noticeTypeMapper.selectByPrimaryKey(nextId);
            //更新下一条记录的sort为当前值
            noticeType.setId(nextId - 1);
            noticeType.setSort(nextNotice.getSort() + 1 );
            noticeTypeMapper.updateSortById(noticeType);
            //更新当前记录的sort为下一条
            nextNotice.setId(orderReq.getId());
            noticeType.setSort(nextNotice.getSort() + 1 );
            noticeTypeMapper.updateSortById(nextNotice);
        }
        // 3、下移动
        if(orderReq.getOperaType().equals(2)){
            //查询上一条记录Id和sort
            Integer previousId = noticeTypeMapper.selectPreviousId(noticeType);
            NoticeType previousNotice =  noticeTypeMapper.selectByPrimaryKey(previousId);
            //更新上一条记录的sort为当前值
            noticeType.setId(previousId);
            noticeType.setSort(previousNotice.getSort() + 1 );
            noticeTypeMapper.updateSortById(noticeType);
            //更新当前记录的sort为上一条
            previousNotice.setId(orderReq.getId());
            noticeType.setSort(previousNotice.getSort() - 1 );
            noticeTypeMapper.updateSortById(previousNotice);
        }
        return 1;
    }
}
